<!DOCTYPE html>
<html lang='en'>
<head>
  <meta charset='UTF-8'>
  <title>Title</title>
</head>
<body>
<script>
  window.onload = function() {
    // 异步处理
    const doMulti = num => {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          if (num) {
            resolve(num * num)
          } else {
            reject(new Error('num not specified'))
          }
        }, 2000)
      })
    }
    // 主函数
    const main = () => {
      console.log('start');
      const nums = [1, 2, 3];
      nums.forEach(async (x) => {
        const res = await doMulti(x);
        console.log(res);
      });
      console.log('end');
    };

    const main2 = async () => {
      console.log('start');
      const nums = [1, 2, 3];
      for (const x of nums) {
        const res = await doMulti(x);
        console.log(res);
      }
      console.log('end');
    };

    main()
  }
</script>

</body>
</html>
